package br.uece.tcc.flathead.jxta;

import java.io.IOException;

import net.jxta.endpoint.Message;
import net.jxta.endpoint.StringMessageElement;
import net.jxta.peergroup.PeerGroup;
import net.jxta.pipe.InputPipe;
import net.jxta.pipe.OutputPipe;
import net.jxta.pipe.OutputPipeEvent;
import net.jxta.pipe.OutputPipeListener;
import net.jxta.pipe.PipeService;
import net.jxta.protocol.PipeAdvertisement;
import net.jxta.util.JxtaServerPipe;

/*
 * Broadcast 
 * Created on 30/06/2005
 * 
 */

/**
 * @author edward ribeiro
 *
 */
public class FhJxtaBroadcast {
	
	private PeerGroup group;
	private PipeAdvertisement pipeAdv;
	private JxtaServerPipe server;
	private PipeService pipeService;
	private InputPipe inPipe; 
	private String peerName;
	public static final String PUBLIC = "MESSAGE:PUBLIC";
	
	public FhJxtaBroadcast(PeerGroup group, PipeAdvertisement padv, FhJxtaBroadcastListener listener) throws IOException {
		pipeAdv     = padv;		
		peerName    = group.getPeerName();
		pipeService = group.getPipeService();		
		inPipe  = pipeService.createInputPipe(padv, listener);
	}
	
	public void sendMessage(String message) throws IOException {
		if (message.length() > 0) {
			message = peerName + ": " + message;		
			OutputPipe outPipe = pipeService.createOutputPipe(pipeAdv, 10000);
			Message msg = new Message();		
			msg.addMessageElement(FhJxtaBroadcast.PUBLIC, new StringMessageElement(FhJxtaBroadcast.PUBLIC, message, null));	
			outPipe.send(msg);			
		}
	}
}
